package org.dromara.system.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.BeanCopyUtils;
import org.dromara.system.mapper.SysUserRoleMapper;
import org.dromara.system.pojo.bo.SysUserRoleBO;
import org.dromara.system.pojo.entity.SysUserRoleDO;
import org.dromara.system.pojo.qo.SysUserRoleQO;
import org.dromara.system.pojo.vo.SysUserRoleVO;
import org.dromara.system.service.SysUserRoleService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户和角色关联表 服务层实现。
 *
 * @author mkj
 * @since 2024-07-21
 */
@RequiredArgsConstructor
@Service
public class SysUserRoleServiceImpl implements SysUserRoleService {

    private final SysUserRoleMapper sysUserRoleMapper;

    @Override
    public boolean add(SysUserRoleBO sysUserRoleBO) {
        SysUserRoleDO sysUserRoleDO = BeanCopyUtils.copy(sysUserRoleBO, SysUserRoleDO.class);
        return sysUserRoleMapper.insert(sysUserRoleDO) > 0;
    }

    @Override
    public boolean remove(Long id) {
        return sysUserRoleMapper.deleteById(id) > 0;
    }

    @Override
    public boolean update(SysUserRoleBO sysUserRoleBO) {
        SysUserRoleDO sysUserRoleDO = BeanCopyUtils.copy(sysUserRoleBO, SysUserRoleDO.class);
        return sysUserRoleMapper.update(sysUserRoleDO) > 0;
    }

    @Override
    public List<SysUserRoleVO> list(SysUserRoleQO sysUserRoleQO) {
        SysUserRoleDO sysUserRoleDO = BeanCopyUtils.copy(sysUserRoleQO, SysUserRoleDO.class);
        QueryWrapper queryWrapper = QueryWrapper.create(sysUserRoleDO);
        return sysUserRoleMapper.selectListByQueryAs(queryWrapper, SysUserRoleVO.class);
    }

    @Override
    public SysUserRoleVO getInfo(Long id) {
        SysUserRoleDO sysUserRoleDO = sysUserRoleMapper.selectOneById(id);
        if (sysUserRoleDO == null) {
            return null;
        }
        return BeanCopyUtils.copy(sysUserRoleDO, SysUserRoleVO.class);
    }

    @Override
    public Page<SysUserRoleVO> page(SysUserRoleQO page) {
        return sysUserRoleMapper.paginateAs(page.getPageNumber(), page.getPageSize(), QueryWrapper.create(page), SysUserRoleVO.class);
    }

}

